package com.haiertvbic.tvnow.pop;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.haiertvbic.tvnow.OTTInfoActivity;
import com.haiertvbic.tvnow.R;
import com.haiertvbic.tvnow.adapter.ActotDetailAdapter;
import com.haiertvbic.tvnow.model.Actor;
import com.haiertvbic.tvnow.model.ProgramDetail;
import com.haiertvbic.tvnow.network.EPGWeb;
import com.haiertvbic.tvnow.network.ImageUtil;
import com.haiertvbic.tvnow.util.SLog;

/**
 * 加上演员作品
 * 
 * @author sun
 * 
 */
public class ActorDetailPop extends PopWindow implements OnKeyListener {
	private static final String TAG = "ActorDetailPop";
	private Activity context;
	private View childView;
	private PopupWindow pop;
	private Actor actor;
	private ProgramDetail program;

	public ActorDetailPop(Activity context, Actor actor, View childView,
			PopupWindow pop, ProgramDetail program) {
		this.context = context;
		this.actor = actor;
		this.childView = childView;
		this.pop = pop;
		this.program = program;
	}

	@Override
	public void showPop() {
		dismissPop();
		new ActorDetailAsyncTask().execute(actor);
	}

	@Override
	public void dismissPop() {
		if (pop != null && pop.isShowing()) {
			pop.dismiss();
			pop = null;
		}
	}

	class ViewHodler {
		ProgressBar pb;

		LinearLayout actorDb;
		ImageView actorPhoto;
		TextView actorName;
		TextView actorSex;
		TextView actorBirthday;
		ListView actorResume;
	}

	public class PopOnKeyListener implements OnKeyListener {

		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_BACK:
			case KeyEvent.KEYCODE_DPAD_LEFT:
				pop.setFocusable(false);
				dismissPop();
				new ProgramDetailPop(context, childView, program).showPop();
				break;

			default:
				break;
			}
			return false;
		}

	}

	public class ActorDetailAsyncTask extends AsyncTask<Object, Void, Void> {
		ViewHodler hodler = new ViewHodler();
		private Actor actor;
		private Bitmap actorPhoto;
		private ArrayList<ProgramDetail> products;

		@Override
		protected void onPreExecute() {
			View connvert = LayoutInflater.from(context).inflate(
					R.layout.pop_actor_detail, null);
			// AlphaAnimation animIn = new AlphaAnimation(0, 1);
			// animIn.setDuration(1000);
			// connvert.startAnimation(animIn);
			Animation inAnimation = AnimationUtils.loadAnimation(context,
					R.anim.slide_in_right);
			inAnimation.setStartOffset(100);
			connvert.startAnimation(inAnimation);
			int[] arrayOfInt = new int[2];
			if (childView != null) {
				childView.getLocationInWindow(arrayOfInt);
				float pictureHeight = context.getResources().getDimension(
						R.dimen.picture_height);
				float pictureWidth = context.getResources().getDimension(
						R.dimen.picture_width);
				pictureWidth = 450f;
				pictureHeight = 230f;

				// 当条目被选中弹出popwindow
				pop = new PopupWindow(connvert, (int) pictureWidth,
						(int) (pictureHeight + 0.5f));
				pop.setBackgroundDrawable(new BitmapDrawable());
				pop.setFocusable(true);
				pop.showAtLocation(childView, Gravity.LEFT | Gravity.TOP,
						(int) (0 - pictureWidth), arrayOfInt[1]);
				hodler.actorDb = (LinearLayout) connvert
						.findViewById(R.id.actor_detail);
				hodler.pb = (ProgressBar) connvert
						.findViewById(R.id.progressBar_js);
				hodler.actorPhoto = (ImageView) connvert
						.findViewById(R.id.actor_photo);
				hodler.actorName = (TextView) connvert
						.findViewById(R.id.actor_name);
				hodler.actorSex = (TextView) connvert
						.findViewById(R.id.actor_sex);
				hodler.actorBirthday = (TextView) connvert
						.findViewById(R.id.actor_birthday);
				hodler.actorResume = (ListView) connvert
						.findViewById(R.id.actor_resume);
			}
			hodler.actorResume.setOnKeyListener(new PopOnKeyListener());
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Object... params) {
			Actor actorP = (Actor) params[0];
			actor = EPGWeb.getActorInfo(context, actorP.getStarID());
			String name = null;
			try {
				name = URLEncoder.encode(actorP.getStarName(), "utf-8");
				SLog.d(TAG, "URLEncoder.encode==" + name);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			products = EPGWeb.getActorProducts(context, name);
			if (actor != null && actor.getPhotoUrl() != null) {
				String photoUrl = actor.getPhotoUrl().trim();
				actorPhoto = ImageUtil.getPicData(photoUrl);
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			hodler.pb.setVisibility(View.GONE);
			if (actor != null) {
				if (actorPhoto != null) {
					hodler.actorPhoto.setImageBitmap(actorPhoto);
				}
				hodler.actorDb.setVisibility(View.VISIBLE);
				String name = context.getString(R.string.actor_name)
						+ actor.getStarName();
				hodler.actorName.setText(name);
				String sex = context.getString(R.string.actor_sex)
						+ actor.getSex();
				hodler.actorSex.setText(sex);
				String birthday = context.getString(R.string.actor_birthday)
						+ actor.getBirthday();
				hodler.actorBirthday.setText(birthday);
				// ========
				String resume = context.getString(R.string.actor_resume);
				resume = actor.getResume();
				ArrayList<String> prods = new ArrayList<String>();
				for (int i = 0; products != null && i < products.size(); i++) {
					prods.add(products.get(i).getPn());
				}
				// prods.add("极限追捕");
				// prods.add("武朝迷案");
				prods.add(resume);
				hodler.actorResume.setAdapter(new ActotDetailAdapter(prods,
						context));
				if (products != null && products.size() > 0) {
					hodler.actorResume
							.setOnItemClickListener(new ProdustsItemClickListener(
									products));
				}
				// ===========
			} else {
				Toast.makeText(context,
						context.getString(R.string.no_relatve_detail), 1)
						.show();
				dismissPop();
			}
		}
	}

	public class ProdustsItemClickListener implements OnItemClickListener {
		private ArrayList<ProgramDetail> actorProducts;

		public ProdustsItemClickListener(ArrayList<ProgramDetail> products) {
			// TODO Auto-generated constructor stub
			this.actorProducts = products;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			if (actorProducts.size() < (position + 1)) {
				return;
			}
			pop.setFocusable(false);
			dismissPop();
			Intent intent = new Intent();
			intent.setClass(context, OTTInfoActivity.class);
			intent.putExtra("PRO_NAME", actorProducts.get(position).getPn());
			intent.putExtra("PRO_TYPE", actorProducts.get(position).getPtype());
			intent.putExtra("PRO_EPISODE", actorProducts.get(position).getJs());
			intent.putExtra("StartActivity", "ProgramListActivity");
			context.startActivity(intent);
			Toast.makeText(context, "跳到跨频道搜索，pid是==", 1).show();
		}

	}

	@Override
	public boolean getPopState() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setPopState(boolean b) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		return false;
	}

}
